package com.turing.procedure.controller;

import com.netflix.discovery.converters.Auto;
import com.turing.common.entity.Context;
import com.turing.common.entity.UserToken;
import com.turing.common.response.PageBean;
import com.turing.common.response.ResponseCode;
import com.turing.common.response.ResponseMessage;
import com.turing.common.response.ResponseVO;
import com.turing.common.util.AssertUtil;
import com.turing.common.util.ResponseUtil;
import com.turing.common.util.StringUtils;
import com.turing.common.util.Tools;
import com.turing.procedure.client.draw.DrawInnerService;
import com.turing.procedure.dao.task.TaskRecordMapper;
import com.turing.procedure.enums.TaskStatusEnum;
import com.turing.procedure.model.domain.task.ReviewRecord;
import com.turing.procedure.model.domain.task.TaskRecord;
import com.turing.procedure.model.domain.task.TaskRecordDto;
import com.turing.procedure.service.task.ReviewRecordService;
import com.turing.procedure.service.task.TaskRecordService;
import com.turing.procedure.vo.in.ReviewContentVo;
import com.turing.procedure.vo.in.ReviewRecordVo;
import com.turing.procedure.vo.out.FileListVO;
import com.turing.procedure.vo.in.FileSearchVO;
import com.turing.procedure.vo.in.TaskRecordVo;
import com.turing.procedure.vo.out.FilePageBean;
import io.swagger.annotations.Api;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Api("任务流程功能入口")
@RestController
@RequestMapping("process")
public class ProcedureController {

    @Autowired
    private TaskRecordService taskRecordService;

    @Autowired
    private ReviewRecordService reviewRecordService;

    private static final Log logger = LogFactory.getLog(ProcedureController.class);

    @GetMapping("test")
    public void test(  ){

        UserToken tokenContext = Context.getTokenContext();
        logger.info(tokenContext.toString());

    }

    @PostMapping("createTaskRecords")
    public ResponseVO<Object> createTaskRecords( @RequestBody List<TaskRecordVo> taskRecordVoList ){

        if(Tools.isEmpty(taskRecordVoList)){
            return ResponseUtil.failed(ResponseCode.PARAMS_REQUIRED, ResponseMessage.PARAMS_REQUIRED);
        }

        for (TaskRecordVo taskRecordVo : taskRecordVoList) {
            logger.info("<<<< taskRecordVoList "+taskRecordVoList);
            TaskRecord taskRecord = new TaskRecord();
            BeanUtils.copyProperties(taskRecordVo,taskRecord);
            taskRecord.setUploadTime(new Date());
            logger.info("<<<<  "+taskRecord);
            taskRecordService.save(taskRecord);

        }

        return ResponseUtil.success();

    }

    @PostMapping("getTaskRecordList")
    public ResponseVO<Object> getDrawFileList(@RequestBody FileSearchVO searchVO) {
        return ResponseUtil.success(taskRecordService.getTaskRecordList(searchVO));
    }

    @PostMapping("requireReviewTask")
    public ResponseVO<String> requireReviewTask(@RequestParam("taskSn") String taskSn) {
        AssertUtil.isEmpty(taskSn, "任务不能为空");
        ResponseVO<ReviewContentVo> responseVO = taskRecordService.batchGetReviewContent(Collections.singletonList(taskSn));
        return ResponseUtil.success(responseVO.getData().getUrl() + "?parentTaskSn=" + taskSn);
    }

    /*
     * 完成审核
     * */
    @PostMapping("finishReview")
    public ResponseVO<Boolean> finishReview(@RequestParam("taskSn") String taskSn){
        return ResponseUtil.success(taskRecordService.finishReview(taskSn));
    }

    @PostMapping("/preTaskFinished")
    public ResponseVO<Boolean> preTaskFinished(@RequestParam("taskSn") String taskSn){

        return ResponseUtil.success(taskRecordService.updateTaskStatus(TaskStatusEnum.PRETASKFINISHED.getCode(),taskSn));

    }

    @PostMapping("/OCRTaskFinished")
    public ResponseVO<Boolean> OCRTaskFinished(@RequestParam("taskSn") String taskSn){

        return ResponseUtil.success(taskRecordService.updateTaskStatus(TaskStatusEnum.OCRFINISHED.getCode(),taskSn));

    }

    @PostMapping("/forecastFinished")
    public ResponseVO<Boolean> forecastFinished(@RequestParam("taskSn") String taskSn){

        return ResponseUtil.success(taskRecordService.updateTaskStatus(TaskStatusEnum.WAITINGFORREVIEW.getCode(),taskSn));

    }

    @PostMapping("/updateTaskStatus")
    public ResponseVO<Boolean> updateTaskStatus( @RequestParam("taskSn") String taskSn,@RequestParam("status") Integer status ){

        return ResponseUtil.success(taskRecordService.updateTaskStatus(status,taskSn));

    }

    @PostMapping("/taskFailed")
    public ResponseVO<Boolean> taskFailed(@RequestParam("taskSn") String taskSn,@RequestParam("errorMessage") String errorMessage){

        return ResponseUtil.success(taskRecordService.taskFailed(taskSn,errorMessage));

    }

    @PostMapping("getTaskRecord")
    public ResponseVO<List<TaskRecordVo>> getTaskRecord(@RequestBody TaskRecordDto taskRecordDto){
        return ResponseUtil.success(taskRecordService.getTaskRecord(taskRecordDto));
    }

    @PostMapping("removeTasks")
    public ResponseVO<Object> removeProcessTask(@RequestBody List<String> taskSns){
        return ResponseUtil.success(taskRecordService.removeProcessTasks(taskSns));
    }

    @PostMapping("addReviewRecords")
    public ResponseVO<Object> addReviewRecords(@RequestBody ReviewRecordVo reviewRecordVo){

        return ResponseUtil.success(reviewRecordService.addReviewRecords(reviewRecordVo));

    }

    @PostMapping("getPreviousAndNext")
    public ResponseVO<Object> getPreviousAndNext(@RequestBody FileListVO fileListVO){

        if( StringUtils.isEmpty(fileListVO.getTaskSn()) || StringUtils.isEmpty(fileListVO.getUploadTime()) ){
            return ResponseUtil.failed(ResponseMessage.PARAMS_REQUIRED);
        }

        Map<String, Integer> privileges = Context.getTokenContext().getTemplates();
        if(Tools.isEmpty(privileges)){
            return ResponseUtil.failed(ResponseMessage.INSUFFICIENT_PRIVILEGES);
        }

        return ResponseUtil.success(taskRecordService.getPreviousAndNext(fileListVO,privileges));

    }


}
